package com.xjm.entity.vo;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjm.enums.StatusEnum;
import lombok.Data;

@Data
public class Result<T> {
    private Integer code;
    private String msg;
    private T data;

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> Result<T> success() {
        return success(StatusEnum.Success.msg);
    }

    public static <T> Result<T> success(String msg, T data) {
        return new Result<T>(StatusEnum.Success.code, msg, data);
    }

    public static <T> Result<T> success(String msg) {
        return new Result<T>(StatusEnum.Success.code, msg, null);
    }

    public static <T> Result<T> success(T data) {
        return new Result<T>(StatusEnum.Success.code, StatusEnum.Success.msg, data);
    }

    public static <T> Result<PageVo<T>> success(IPage<T> paged) {
        PageVo<T> pageVo = new PageVo<>(paged.getCurrent(), paged.getSize());
        pageVo.setTotal(paged.getTotal());
        pageVo.setList(paged.getRecords());
        return new Result<>(StatusEnum.Success.code, StatusEnum.Success.msg, pageVo);
    }

    public static <T> Result<T> error(String msg) {
        return new Result<T>(StatusEnum.Fail.code, msg, null);
    }

    public static <T> Result<T> error(Integer code, String msg) {
        return new Result<T>(code, msg, null);
    }

    public static <T> Result<T> error(StatusEnum statusEnum) {
        return new Result<T>(statusEnum.code, statusEnum.msg, null);
    }

    public static <T> Result<T> error() {
        return new Result<>(StatusEnum.Fail.code, StatusEnum.Fail.msg, null);
    }

    public static <T> Result<T> data(StatusEnum statusEnum, T data) {
        return new Result<T>(statusEnum.code, statusEnum.msg, data);
    }
}
